package cn.itcast.travel.service.impl;

import cn.itcast.travel.domain.comment;
import cn.itcast.travel.service.CommentService;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CommentServiceImpl implements CommentService {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //获取博客的评论
    @Override
    public List<comment> getallcomment(String id) {
        List<comment> l=null;
        String sql="select *,t_comment.id tcid from t_comment join t_blogger tb on tb.id = t_comment.bloggerid where blogid=?";
        l=template.query(sql,new BeanPropertyRowMapper<comment>(comment.class),id);
        return l;
    }

    //点赞或踩
    @Override
    public void dianzancai(String w, String id) {
        if(w.equals("-1")){
            String sql="update t_comment set dianzan=dianzan+1 where id=?";
            template.update(sql,id);
        }else{
            String sql="update t_comment set biandi=biandi+1 where id=?";
            template.update(sql,id);
        }
    }

    //添加评论
    @Override
    public void addpin(comment c) {
        String sql="insert into t_comment(content,commentDate,bloggerid,blogid) value(?,?,?,?)";
        template.update(sql,c.getContent(),c.getCommentDate(),c.getBloggerid(),c.getBlogid());
    }
}
